package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.bean.ProjectTree
import com.hinnotek.warehouse.databinding.ActivityLossReportBinding
import com.hinnotek.warehouse.databinding.ItemLossReportLayoutBinding
import com.hinnotek.warehouse.databinding.ItemStockInLayoutBinding
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_4
import com.hinnotek.warehouse.viewmodel.StockInViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.*
import dagger.hilt.android.AndroidEntryPoint

/**
 * 报损提交页面
 */
@Route(path = ACTIVITY_WAREHOUSE_LOSS_REPORT)
@AndroidEntryPoint
class LossReportActivity : BaseActivity<ActivityLossReportBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineAccountBean, ItemLossReportLayoutBinding>

    private val viewModel by viewModels<StockInViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_loss_report)
        setTitle("报损")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.applyType = WAREHOUSE_TYPE_4
    }

    override fun onResume() {
        viewModel.autoRefresh()
        super.onResume()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(R.layout.item_loss_report_layout, ArrayList(), BR.lossReport)
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
        //添加adapter
        adapter.addListener { binding, item ->
            binding.root.setOnClickListener {
                Skipping.start(ACTIVITY_WAREHOUSE_LOSS_REPORT_LIST,"applyId",item.applyId,MACHINE_CONTENT_LIST,item,this@LossReportActivity)
            }
        }
        //新增跳转
        setRightText(getString(R.string.addwarehouse))?.setOnClickListener {
            Skipping.start(ACTIVITY_WAREHOUSE_NEW_STOCK_IN,"applyType",WAREHOUSE_TYPE_4,this@LossReportActivity)
        }
        //搜索
        bindingView.search.setOnClickListener {
            viewModel.setParameters(bindingView.topView.text.toString())
            viewModel.autoRefresh()
        }

    }

    override fun setData() {
        //加载数据
        viewModel.stockInPage.observe(this, Observer {
            adapter.addAll(it, viewModel.isFirst)
        })
    }

}